<template>
	<view class="app">
		<view class="app-1">
			<view class="app-img">
				<text>头像</text>
				<view class="">
					<view class=""><image :src="user.headimgurl" mode="aspectFill"></image></view>
					<text class="iconfont">&#xe60d;</text>
				</view>
			</view>
			<view class="aapp-item">
				<text>用户昵称</text>
				<view class="">
					<view class="">
						<text>{{ user.nickname }}</text>
					</view>
					<text class="iconfont">&#xe60d;</text>
				</view>
			</view>
		</view>
		<view class="blank"></view>
		<view class="app-1">
			<!-- <view class="aapp-item">
				<text>用户ID</text>
				<view class="">
					<view class=""><text>刘彦祖</text></view>
				</view>
			</view> -->
			<view class="app-item">
				<text>手机号</text>
				<view class="">
					<view class="">
						<text v-if="user.frontType">{{ user.phone }}</text>
						<text v-else>去授权</text>
						<button v-if="!user.frontType" type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button>
						<!-- <button v-if="!user.frontType" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" withCredentials="true">获取手机号码</button> -->
					</view>
					<text class="iconfont">&#xe60d;</text>
				</view>
			</view>
			<view class="app-item">
				<text>我的身份</text>
				<view class="">
					<view class="">
						<text v-if="user.frontType == 0" @tap="show = true">普通用户</text>
						<text v-else-if="user.frontType == 1">委员</text>
						<text v-else>需授权</text>
					</view>
					<text class="iconfont">&#xe60d;</text>
				</view>
			</view>
		</view>
		<view class="blank"></view>
		<view class="app-1" v-if="user.frontType == 1">
			<view class="aapp-item">
				<text>出生年月</text>
				<view class="">
					<view class="">
						<text>{{ user.birthday || '暂无' }}</text>
					</view>
				</view>
			</view>
			<view class="aapp-item">
				<text>性别</text>
				<view class="">
					<view class="">
						<text>{{ user.sex == 1 ? '男' : (user.sex == 1) == 2 ? '女' : '保密' }}</text>
					</view>
				</view>
			</view>
			<view class="aapp-item">
				<text>现任（或原任）职务</text>
				<view class="">
					<view class="">
						<text>{{ user.job || '暂无' }}</text>
					</view>
				</view>
			</view>
			<view class="aapp-item">
				<text>籍贯</text>
				<view class="">
					<view class="">
						<text>{{ user.nativePlace || '暂无' }}</text>
					</view>
				</view>
			</view>
			<view class="aapp-item">
				<text>学历学位</text>
				<view class="">
					<view class="">
						<text>{{ user.education || '暂无' }}</text>
					</view>
				</view>
			</view>
			<view class="aapp-item">
				<text>民族</text>
				<view class="">
					<view class="">
						<text>{{ user.political || '暂无' }}</text>
					</view>
				</view>
			</view>
		</view>
		<u-mask :show="show" @tap="show = false">
			<view class="mark">
				<view class="mark-1" @tap.stop>
					<text>请输入您留在委员库 中的手机号</text>
					<view class=""><u-input v-model="formData.phone" type="text" placeholder="请输入手机号..." /></view>
					<view class=""><u-input v-model="formData.name" type="text" placeholder="请输入姓名..." /></view>
					<view class="btn"><text @tap.stop="onSumbit">提交</text></view>
				</view>
				<view class="mark-2" @tap="show = false"><image src="https://gnzxwylz.xlygrcw.com/upload/GNZX/user/10.png"></image></view>
			</view>
		</u-mask>
	</view>
</template>

<script>
export default {
	data() {
		return {
			show: false,
			user: {},
			formData: {},
			wxData: {}
		};
	},
	onShow() {
		
	},
	onLoad() {
		this.user = uni.getStorageSync('gnzx_users');
		var that = this;
		uni.login({
			provider: 'weixin',
			success: function(res) {
				that.request({
					url: that.interfaces.getOpenId,
					data: {
						code: res.code
					},
					success: user => {
						that.wxData = user;
					},
					fail(e) {
						uni.showToast({
							title: '登录失败'
						});
					}
				});
			}
		});
	},
	methods: {
		decryptPhoneNumber(e) {
			console.log(e);

			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				const data = {};
				data.encryptedData = e.detail.encryptedData;
				data.iv = e.detail.iv;
				data.sessionKey = this.wxData.session_key;
				const param = uni.getStorageSync('gnzx_users');
				data.id = param.id;
				this.getPhone(data);
			} else {
				console.log('用户点击了拒绝');
			}
		},
		getPhone(data) {
			var that = this;
			this.request({
				url: this.interfaces.getUserPhone,
				data: data,
				success: res => {
					console.log(res);
					if (res.data) {
						that.getCommitteeApply({ phone: res.data, userId: that.user.id });
						return false;
					} else {
						uni.showToast({
							icon: 'none',
							title: '授权失败，请重新授权'
						});
					}
				},
				fail(e) {
					uni.showToast({
						title: '获取失败'
					});
				}
			});
		},
		getCommitteeApply(data) {
			console.log(data);
			var _this = this;
			this.request({
				url: _this.interfaces.getCommitteeApply,
				data: data,
				method: 'POST',
				success: res => {
					console.log(res);
					uni.showToast({
						title: res.message || '获取失败',
						icon: 'none'
					});
					_this.getNewUserInfo();
				},
				fail(e) {
					console.log(e);
					uni.showToast({
						title: '获取失败'
					});
				}
			});
		},
		getNewUserInfo() {
			const param = uni.getStorageSync('gnzx_users');
			this.request({
				url: this.interfaces.getNewUserInfoByOpenId,
				data: param,
				method: 'POST',
				success: res1 => {
					console.log(res1.data);
					this.user = res1.data;
					uni.setStorageSync('gnzx_users', res1.data);
				},
				fail(e) {
					console.log(e);
					uni.showToast({
						title: '获取失败'
					});
				}
			});
		},
		onSumbit() {
			var _this = this;
			this.formData.userId = this.user.id;
			if (this.result.isWeiyuan(this.formData)) {
				this.request({
					url: _this.interfaces.getCommitteeInputRzApply,
					data: _this.formData,
					method: 'POST',
					success: res => {
						uni.showToast({
							title: res.data || '获取失败',
							icon: 'none',
							duration: 2000
						});
						setTimeout(function() {
							_this.getNewUserInfo();
						}, 1000);

						_this.user = res.data;
						_this.show = false;
					}
				});
			}
		}
	}
};
</script>

<style lang="scss">
@import './index.scss';
</style>
